/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.sys.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.sys.entity.RegularTask;
import com.thinkgem.jeesite.modules.sys.service.RegularTaskService;

/**
 * 定时任务Controller
 * @author Dean
 * @version 2017-03-08
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/regularTask")
public class RegularTaskController extends BaseController {

	@Autowired
	private RegularTaskService regularTaskService;
	
	@ModelAttribute
	public RegularTask get(@RequestParam(required=false) String id) {
		RegularTask entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = regularTaskService.get(id);
		}
		if (entity == null){
			entity = new RegularTask();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:regularTask:view")
	@RequestMapping(value = {"list", ""})
	public String list(RegularTask regularTask, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<RegularTask> page = regularTaskService.findPage(new Page<RegularTask>(request, response), regularTask); 
		model.addAttribute("page", page);
		return "modules/sys/regularTaskList";
	}

	@RequiresPermissions("sys:regularTask:view")
	@RequestMapping(value = "form")
	public String form(RegularTask regularTask, Model model) {
		model.addAttribute("regularTask", regularTask);
		return "modules/sys/regularTaskForm";
	}

	@RequiresPermissions("sys:regularTask:edit")
	@RequestMapping(value = "save")
	public String save(RegularTask regularTask, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, regularTask)){
			return form(regularTask, model);
		}
		regularTaskService.save(regularTask);
		addMessage(redirectAttributes, "保存定时任务成功");
		return "redirect:"+Global.getAdminPath()+"/sys/regularTask/?repage";
	}
	
	@RequiresPermissions("sys:regularTask:edit")
	@RequestMapping(value = "delete")
	public String delete(RegularTask regularTask, RedirectAttributes redirectAttributes) {
		regularTaskService.delete(regularTask);
		addMessage(redirectAttributes, "删除定时任务成功");
		return "redirect:"+Global.getAdminPath()+"/sys/regularTask/?repage";
	}

}